#ifndef SERIAL_H_
#define SERIAL_H_

/*
  Library for UART handling.
  Ref:
    LPC2378 User manual
*/
#include <stdio.h>
#include <targets/lpc2378.h>
#include <targets/liblpc2000.h>
#include <cross_studio_io.h>
#include <ctl_api.h>

#include "timer.h"
#include "../utils/buffer.h"

#define COM1_BAUD 9600
#define COM2_BAUD 9600
#define COM3_BAUD 9600

#define IIR1 1
#define IIR2 2
#define IIR3 3

#define SERIAL_BUFFER_SIZE 64 + 1

typedef enum {
  COM0, //UART0
  COM2, //UART2
  COM3, //UART3
} SerialPort_t;


/* Initialize serial port. Set baud=0 for autobaud */
void serial_enable(SerialPort_t port, int baud);
/* Get number of available bytes */
int  serial_available(SerialPort_t port);
/* Read bytes from buffer. Returns number of readed bytes. */
int  serial_read(SerialPort_t port, char* dest, int length);
/* Put char into serial port */
void serial_putc(SerialPort_t port, unsigned char c);
/* Put string into serial port */
void serial_puts(SerialPort_t port, unsigned char *str);
void serial_putn(SerialPort_t port, unsigned char *buffer, int length);

/* Flush buffer */
void serial_flush(SerialPort_t port);

/* Catch data in transparent mode */
void serial_catch(SerialPort_t port);

/* Release data in transparent mode */
void serial_release(SerialPort_t port);

/* Close port */
void serial_close(SerialPort_t port);

#endif